package com.study.manage.mvc.org.memo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.study.manage.dal.EvaluateGroupDetailRepository;
import com.study.manage.dal.EvaluateRepository;
import com.study.manage.dal.MemoRepository;
import com.study.manage.domain.Evaluate;
import com.study.manage.domain.Memo;
import com.study.manage.dto.JsonResponseBean;
import com.study.manage.mvc.common.BaseController;
import com.study.manage.utils.CmnStringUtils;
import com.study.manage.utils.MapUtils;

@Controller
@RequestMapping("org/evaluate/memo")
public class MemoController extends BaseController {

	@Autowired
	private MemoRepository memoRepository;
	
	@Autowired
	private EvaluateRepository evaluateRepository;
	
	@Autowired
	private EvaluateGroupDetailRepository evaluateGroupDetailRepository;
	
    @RequestMapping(path = "init", method = RequestMethod.GET)
    public String init(Long id,String flgs,ModelMap model) {
    	
    	model.put("id", id);
    	model.put("flgs", flgs);
    	if("0".equals(flgs)){//比赛组
    		List<Long> evaluateIds = evaluateGroupDetailRepository.findEvaluateIdByGroupId(id);
    		List<Map<String,String>> list = new ArrayList<>();
    		for(int i=0;i<evaluateIds.size();i++){
    			Evaluate evaluate = evaluateRepository.findOne(evaluateIds.get(i));
    			Map<String,String> map = new HashMap<>();
    			map.put("id", evaluateIds.get(i).toString());
    			map.put("name", evaluate.title);
    			list.add(map);
    		}
    		model.put("list", list);
    	}else{//比赛
    		Evaluate evaluate = evaluateRepository.findOne(id);
    		model.put("bsName", evaluate.title);
    	}
        return "org/evaluate/memo";
    }

    @ResponseBody
    @RequestMapping(path = "getWorkData", method = RequestMethod.GET)
    public JsonResponseBean getPageDatas(Long groupId,String flgs,String timeFrom,String timeTo,String context,
            @PageableDefault(sort = {"createdAt"}, direction = Direction.DESC) Pageable pageable) {
    	
    	List<Long> evaluateIds = null;
    	if("0".equals(flgs)){
    		 evaluateIds = evaluateGroupDetailRepository.findEvaluateIdByGroupId(groupId);
    	}else{
    		evaluateIds=new ArrayList<>(Arrays.asList(groupId));
    	}
        Page<Memo> page = memoRepository.getMemoDatas(evaluateIds,timeFrom,timeTo, CmnStringUtils.isNotEmpty(context) ? "%"+context+"%" : context ,pageable);
        Object obj = page.map(new Converter<Memo, Map<String, Object>>() {
            @Override
            public Map<String, Object> convert(Memo source) {
            	Map<String, Object> map = MapUtils.convert(source);
            	if(source.evaluateId!=null){
            		Evaluate evaluate = evaluateRepository.findOne(source.evaluateId);
            		map.put("name", evaluate==null ? "" : evaluate.title);
            	}
                return map;
            }
        });
        return new JsonResponseBean(obj,true);
    }
    
    @ResponseBody
    @Transactional
    @RequestMapping(path = "updatememo", method = RequestMethod.POST)
    public JsonResponseBean updateMemo(Long id,String flg,String context) {

    	if("1".equals(flg)){//删除
    		memoRepository.updateState(id);
    	}else if("0".equals(flg)){//编辑
    		memoRepository.updateContext(id,context);
    	}else{//添加
    		memoRepository.insertMemo(id,context);
    	}
    	return new JsonResponseBean(true);
    }
    
}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
